#include<iostream>
using namespace std;

class node {
public:
	int num;
	node* next;
	node(int n);
	~node();
};

node::node(int n) {
	num = n;
	next = nullptr;
}

class list {
	node* head;
	node* tail;
	
public:
	static int sum;
	list() {
		head = tail = nullptr;
	}
	~list();
	void insert(int n);
	void remove(int n);
	void print();
};

int list::sum = 0;

list::~list() {
	node* tempptr;
	while (head->next != NULL)
	{
		tempptr = head->next;
		delete head;
		head = tempptr;
	}
	delete head;
}

void list::print() {
	for (node* i = head; i->next != nullptr;) {
		cout << i->num << " ";
		i = i->next;
	}
}
void list::insert(int n) {
	node* tem = new node(n);
	if (head == nullptr) {
		head = tail = tem;
		sum++;
	}
	else {
		if (n < head->num) {
			tem->next = head;
			head = tem;
			sum++;
			return;
		}
		if (n > tail->num) {
			tail->next = tem;
			tail = tem;
			sum++;
			return;
		}
		node* curr = head;
		while (curr->next != nullptr) {
			if ((curr->num <= n) && (curr->next->num > n)) {
				tem->next = curr->next;
				curr->next = tem;
				sum++;
				return;
			}
			curr = curr->next;
		}
	}
}void list::remove(int n) {
	if (head->num == n) {
		head = head->next;
		sum--;
		return;
	}
	node* tem = head;
	while (tem->next->next != nullptr) {
		if (tem->next->num == n) {
			tem->next = tem->next->next;
			sum--;
			return;
		}
		tem = tem->next;
	}
	if (tem->next->num == n) {
		tail->next = nullptr;
		sum--;
		return;
	}
}


int main() {
	list a;
	int count;
	cout << endl << "Please input the count of the node of the list: ";
	cin >> count;
	srand(time(0));
	for (int i = 0; i < count; i++) {
		int zhong = rand() % 100;
		a.insert(zhong);
	}
	a.print();
	int number;
	cout << "Please input the number of the node you want to insert: ";
	cin >> number;
	a.insert(number);
	a.print();
	cout << endl << "Please input the number of the node you want to delete: ";
	cin >> number;
	a.remove(number);
	a.print();
}
